# -*- coding:utf-8 -*- 
__date__ = '2021/12/25 9:29 上午'

# from 语句让你从模块中导入一个指定的部分到当前命名空间中
from util.list_util import random_list
import time


def bubble_sort(collection):
    length = len(collection)
    for i in range(length - 1):
        swapped = False
        for j in range(length - 1 - i):
            # 如果元素已经排好序，这一次循环不会走，swapped一直为False
            if collection[j] > collection[j + 1]:
                swapped = True
                collection[j], collection[j + 1] = collection[j + 1], collection[j]
                print(collection)
        if not swapped:
            break
    return collection


if __name__ == '__main__':
    # random_list = random_list()
    random_list = [-85, -85, -100, 25, -85, 74, 76, 44, 68, 1]
    start = time.process_time()
    print("random_list", random_list)
    res = bubble_sort(random_list)
    print("time used: {0:10.5f}s".format(time.process_time() - start))
    print(res == sorted(random_list))
